package com.zhongxing.hdy.spritel_hdy.util;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

/**
 * Created by Administrator on 2017年6月30日 0030.
 */

public class AnimUtil {

    public interface AnimListener {
        void down(View view);

        void move(View view);

        void up(View view);
    }

    public static final String PROPERTY_TRANSLATIONY = "translationY";
    public static final String PROPERTY_TRANSLATIONX = "translationX";
    public static final String PROPERTY_ALPHA = "alpha";
    public static final String PROPERTY_SCALEX = "scaleX";
    public static final String PROPERTY_SCALEY = "scaleY";
    public static final String PROPERTY_ROTATIONX = "rotationX";
    public static final String PROPERTY_ROTATIONY = "rotationY";
    public static final String PROPERTY_BACKGROUNDCOLOR = "backgroundColor";

    public static void addObjectAnimation(View view, String[] proNamae, float... values) {
        //创建属性动画
        final int len = proNamae.length;
        PropertyValuesHolder[] holders = new PropertyValuesHolder[len];
        PropertyValuesHolder holder = null;
        for (int i = 0; i < len; i++) {
            holder = PropertyValuesHolder.ofFloat(proNamae[i], values);
            holders[i] = holder;
        }
        ObjectAnimator.ofPropertyValuesHolder(view, holders).setDuration(200).start();
    }

    public static void addOnTouchListener(final View view, final String[] proName, final float[] downValues,
                                          final float[] upValues, final AnimListener listener) {
        if (proName == null) {
            return;
        }


        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //获取当前事件的状态
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN: {
                        Log.i(getClass().getName(), "down");
                        /**
                         *     public ScaleAnimation(float fromX, float toX, float fromY, float toY,
                         int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
                         */
                        addObjectAnimation(v, proName, downValues);
                        if (listener != null) {
                            listener.down(v);
                        }
                        break;
                    }
                    case MotionEvent.ACTION_MOVE: {
                        Log.i(getClass().getName(), "move");

                        if (listener != null) {
                            listener.move(v);
                        }
                        break;
                    }
                    case MotionEvent.ACTION_UP: {
                        Log.i(getClass().getName(), "up");
                        addObjectAnimation(v, proName, downValues);
                        if (listener != null) {
                            listener.up(v);
                        }
                        break;
                    }
                }
                //false只响应一次,true响应所有状态，按下，按住移动，松开，多个手指
                return true;
            }
        });
    }

    public static void addScaleAnimation(View view, float fromX, float toX, float fromY, float toY, final float pvalueX, final float pvalueY) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY,
                Animation.RELATIVE_TO_SELF, pvalueX, Animation.RELATIVE_TO_SELF, pvalueY);
        scaleAnimation.setDuration(200);//持续时间
        scaleAnimation.setFillAfter(true);//保持动画执行完的状态;
        view.startAnimation(scaleAnimation);
    }

    public static void addOnTouchListener(View v, final float pvalueX, final float pvalueY, final AnimListener listener) {
           v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //获取当前事件的状态
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        Log.i("MainActivity", "down");
                        addScaleAnimation(v, 1f, 0.75f, 1f, 0.75f, pvalueX, pvalueY);

                        if (listener != null){
                            listener.down(v);
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.i("MainActivity", "move");
                        if (listener != null){
                            listener.move(v);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i("MainActivity", "up");
                        addScaleAnimation(v, 0.75f, 1f, 0.75f, 1f, pvalueX, pvalueY);

                        if (listener != null){
                            listener.up(v);
                        }
                        break;
                }

                return true;//false只响应一次,true响应所有状态，按下，按住移动，松开，多个手指
            }
        });
    }
}
